package com.xx.ems.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.xx.ems.dao.PermissionsDao;
import com.xx.ems.model.Action;
import com.xx.ems.model.Roles;
import com.xx.ems.model.Users;
import com.xx.ems.system.BaseDao;

@Repository(value="PermissionsDao")
@Transactional
public class PermissionsDaoImpl extends BaseDao implements PermissionsDao {
	@Override
	public void addPermissons(String[] actionids, Roles role, Users loginuser) {
		/**
		 * 设置创建人的信息及创建的时间
		 */
		role.setCreater(loginuser);
		role.setCreation_time(new Date());
		/**
		 * 获取动作信息,并和角色进行关联
		 */
		for (String actionid : actionids) {
			Action action = (Action) getCurrentSession().get(Action.class, Integer.parseInt(actionid));
			action.getRoles().add(role);
			role.getAction().add(action);
		}
		/**
		 * 保存角色相关信息
		 */
		getCurrentSession().save(role);
	}
	
	@Override
	public void updatePermissions(String[] oldactionids, String[] newactionids, Roles role, Users loginuser) {
		/**
		 * 删除指定操作与角色的关联hql
		 */
		String action_role_hql = "delete from Roles_Action ra where ra.fk_aid = :aid";
		Query action_role_query = getCurrentSession().createQuery(action_role_hql);
		/**
		 * 删除旧的操作与角色的关联
		 */
		for (String oldactionid : oldactionids) {
			action_role_query.setInteger("aid", Integer.valueOf(oldactionid));
			action_role_query.executeUpdate();
		}
		/**
		 * 修改角色信息
		 */
		Roles oldrole = (Roles) getCurrentSession().get(Roles.class, role.getRid());
		BeanUtils.copyProperties(oldrole, role);
		/**
		 * 设置修改人的信息及修改时间
		 */
		oldrole.setChanger(loginuser);
		oldrole.setChange_time(new Date());
		/**
		 * 获取动作信息,并和角色进行关联
		 */
		for (String actionid : newactionids) {
			Action action = (Action) getCurrentSession().get(Action.class, Integer.parseInt(actionid));
			action.getRoles().add(role);
			role.getAction().add(action);
		}
		/**
		 * 保存角色相关信息
		 */
		getCurrentSession().update(oldrole);
	}

	@Override
	public void deletePermissions(String roleid) {
		/**
		 * 删除角色的hql
		 */
		String role_hql = "delete from Roles r where r.rid = "+roleid;
		/**
		 * 删除角色与用户关联的hql
		 */
		String role_user_hql = "delete from Users_Roles ur where ur.fk_rid = "+roleid;
		/**
		 * 删除角色与操作关联的hql
		 */
		String role_action_hql = "delete from Roles_Action ra where ra.fk_rid = "+roleid;
		/**
		 * 执行hql语句
		 */
		getCurrentSession().createQuery(role_hql).executeUpdate();
		getCurrentSession().createQuery(role_user_hql).executeUpdate();
		getCurrentSession().createQuery(role_action_hql).executeUpdate();
	}
	
	@Override
	public void authorizeToUsers(String[] userids, String roleid) {
		/**
		 * 获取角色对象
		 */
		Roles role = (Roles) getCurrentSession().get(Roles.class, roleid);
		/**
		 * 获取用户对象,并与角色进行关联
		 */
		for (String userid : userids) {
			Users user = (Users) getCurrentSession().get(Users.class, Integer.valueOf(userid));
			role.getUsers().add(user);
		}
		getCurrentSession().update(role);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Action> getAllPermissions() {
		/**
		 * 获取全部权限 (除了权限管理部分) 的hql
		 */
		String hql = "from Action ac where ac.aid != (select ac1.aid from Action ac1 where ac1.action_name = '权限管理') and " +
				"ac.parent_id != (select ac2.aid from Action ac2 where ac2.action_name = '权限管理')";
		/**
		 * 获取权限
		 */
		List<Action> actionList = (List<Action>) getCurrentSession().createQuery(hql).list();
		
		return actionList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getRolesList(int pageNow, int maxCount) {
		String hql = "from Roles role where role.creation_time desc";
		List<Roles> roleList = getPagingList(pageNow, maxCount, hql);
		List<Object> list = new ArrayList<Object>();
		for (Roles role : roleList) {
			list.add(role.toHashMap());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("totalpage", getTotalpage());
		return map;
	}

	@Override
	public List<Object> getActionsByRoleid(int rid) {
		/**
		 * 根据id获取角色对象
		 */
		Roles role = (Roles) getCurrentSession().get(Roles.class, rid);
		/**
		 * 获取角色所拥有的权限集合
		 */
		Set<Action> actionSet = role.getAction();
		Iterator<Action> it = actionSet.iterator();
		List<Object> list = new ArrayList<Object>();
		while (it.hasNext()) {
			list.add(it.next().toHashMap());
		}
		return list;
	}
}